package leetcode.primary;

public class T66 {
    public int[] plusOne(int[] digits) {
        int len=digits.length;
        if (digits[len-1] !=9){
            digits[len-1]+=1;
            return digits;
        }
        digits[len-1]=0;
        for (int i= len-2;i>=0;i--){
            if (digits[i]!=9){
                digits[i]+=1;
                return digits;
            }
            digits[i] = 0;
        }
        if (digits[0]==0){
            int[] ans = new int[len + 1];
            ans[0] = 1;
            return ans;
        }else {
            return digits;
        }

    }

    public static void main(String[] args) {
        T66 test = new T66();
        int[] plusOne = test.plusOne(new int[]{9999});
        for (int i : plusOne) {
            System.out.print(i);
        }
    }
}
